package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Slf4j
@Api(tags = "地址管理")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    // 地址列表
    @GetMapping("/list")
    public Result<List<AddressBook>> list() {
        List<AddressBook> addressBooks = addressBookService.list();
        return Result.success(addressBooks);
    }

    // 添加地址
    @PostMapping
    public Result<String> save(@RequestBody AddressBook addressBook) {
        addressBookService.save(addressBook);
        return Result.success();
    }

    // 地址详情
    @GetMapping("/{id}")
    public Result<AddressBook> detail(@PathVariable Long id) {
        AddressBook addressBook = addressBookService.detail(id);
        return Result.success(addressBook);
    }

    // 地址修改
    @PutMapping
    public Result<String> update(@RequestBody AddressBook addressBook) {
        addressBookService.update(addressBook);
        return Result.success();
    }

    // 地址删除
    @DeleteMapping
    public Result<String> remove(Long id) {
        addressBookService.removeById(id);
        return Result.success();
    }

    // 设置为默认地址
    @PutMapping("/default")
    public Result<String> setDefault(@RequestBody AddressBook addressBook) {
        addressBookService.setDefault(addressBook.getId());
        return Result.success();
    }

    // 查询默认地址
    @GetMapping("/default")
    public Result<AddressBook> getDefault() {
        AddressBook addressBook = addressBookService.getDefault();
        return Result.success(addressBook);
    }

}
